﻿using Core.Info;
using Core.Utility.Helper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Utility.Config
{
    /// <summary>
    /// 应用设置
    /// </summary>
    public class AppSettingJson : SettingJson
    {
        static AppSettingJson()
        {
            Instance = new AppSettingJson();
        }

        /// <summary>
        /// 实例
        /// </summary>
        public static SettingJson Instance { get; protected set; }

        /// <summary>
        /// 配置名称，需包含文件扩展名
        /// </summary>
        public override string ConfigFileName { get; protected set; } = "app.setting.json";
    }

    /// <summary>
    /// 设置类，用于读取应用Config目录下配置文件信息
    /// </summary>
    public class SettingJson
    {
        /// <summary>
        /// 配置目录
        /// </summary>
        public virtual string ConfigDirectory { get; protected set; } = AppInfo.ConfigDirectory;

        /// <summary>
        /// 配置名称，需包含文件扩展名
        /// </summary>
        public virtual string ConfigFileName { get; protected set; } = "setting.json";

        /// <summary>
        /// 配置
        /// </summary>
        protected JObject Setting { get; set; } = new JObject();

        /// <summary>
        /// 配置文件路径
        /// </summary>
        public virtual string ConfigFilePath
        {
            get
            {
                return Path.Combine(ConfigDirectory, ConfigFileName);
            }
        }

        /// <summary>
        /// 配置
        /// </summary>
        /// <param name="configDirectory"></param>
        /// <param name="configFileName"></param>
        public SettingJson(string? configDirectory = null,string? configFileName = null)
        {
            if(!string.IsNullOrEmpty(configDirectory))
            {
                ConfigDirectory = configDirectory;
            }
            if(!string.IsNullOrEmpty(configFileName))
            {
                ConfigFileName = configFileName;
            }
            if (!File.Exists(ConfigFilePath))
            {
                SaveConfig();
            }
            RefreshConfiguration();
        }

        /// <summary>
        /// 刷新配置，非线程安全
        /// </summary>
        public void RefreshConfiguration()
        {
            try
            {
                Setting = JObject.Parse(FileHelper.ReadFileToString(ConfigFilePath) ?? "{" + Environment.NewLine + "}");
            }
            catch
            {
                Setting = new JObject();
            }
        }

        /// <summary>
        /// 获取配置的值
        /// </summary>
        /// <param name="key">配置节</param>
        /// <returns></returns>
        public virtual JToken? this[string? key]
        {
            get
            {
                JToken? res = null;
                if (!string.IsNullOrEmpty(key))
                {
                    if (Setting.ContainsKey(key))
                    {
                        res = Setting[key];
                    }
                }
                return res;
            }
            set
            {
                if (key != null)
                {
                    Setting[key] = value;
                    SaveConfig();
                }
            }
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        public virtual void SaveConfig()
        {
            FileHelper.WriteFile(ConfigFilePath, JsonHelper.Serialize(Setting) ?? "{" + Environment.NewLine + "}");
        }


    }
}
